#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include "end_of_request.h"
#include "http_server.h"

int end_of_request (http_connection_t *http_connection)
{
#ifdef HAVE_TCP_CORK
	int optval;

	if (http_connection->http_method != HEAD_METHOD) {
		optval = 0;
		setsockopt (http_connection->sd, IPPROTO_TCP, TCP_CORK, &optval, sizeof (int));
	}
#endif /* HAVE_TCP_CORK */

	if (!http_connection->keep_alive) {
		/* Close connection. */
		return -1;
	}

	if (http_server.fn_prepare_for_reading (http_connection) < 0) {
		return -1;
	}

	http_connection_reuse (http_connection);

	return 0;
}
